package com.power.sifa.approve.executor.impl;

import com.plf.common.domain.ResponseDTO;
import com.plf.common.domain.YesNo;
import com.power.flowable.api.ILog_limitscatFeign;
import com.power.flowable.api.IPersonalFeign;
import com.power.sifa.approve.dao.Apl_admre_auditDao;
import com.power.sifa.approve.dao.Apl_limdelay_hearDao;
import com.power.sifa.approve.dao.Apl_limit_hearDao;
import com.power.sifa.approve.model.Apl_limit_hear;
import com.power.sifa.approve.model.pojo.vo.ProcessVariablesVo;
import com.power.sifa.approve.service.*;
import com.power.sifa.util.executor.AbstractPostExecutor;
import com.power.sifa.approve.model.Apl_admre_audit;
import com.power.sifa.approve.model.Apl_admreview;
import com.power.sifa.approve.model.Apl_main;
import com.power.sifa.approve.model.enumeration.Tp_proctype;
import com.power.sifa.approve.model.enumeration.Tp_proctypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * 恢复完成服务
 */
@Component
@Slf4j
public class HearAuditRestore extends AbstractPostExecutor {

    // 用户id
    private String userid;
    // 环节分类标识
    private String category;
    // 用户名
    private String username;
    // 单位id
    private String sectionid;
    // 单位名称
    private String sectionname;
    // 部门id
    private String deptid;
    // 部门名称
    private String deptname;
    // 处理意见
    private String remark;
    // 行政区划
    private String adid;
    // 环节名称
    private String taskname;

    @Resource
    private Apl_admreviewService apl_admreviewService;
    @Resource
    private BusinessApproveService businessApproveService;
    @Resource
    private Apl_admre_auditDao apl_admre_auditDao;
    @Resource
    private Apl_mainService apl_mainService;
    @Resource
    private IPersonalFeign iPersonalFeign;
    @Resource
    private ILog_limitscatFeign lLog_limitscatFeign;
    @Resource
    private Apl_limit_hearDao apl_limit_hearDao;
    @Resource
    private Apl_admre_auditService apl_admre_auditService;


    @Override
    public void init(Map<String, Object> initdata) {
        this.userid = (String) initdata.get("userid");
        this.category = (String) initdata.get("category");
        this.username = (String) initdata.get("username");
        this.sectionid = (String) initdata.get("sectionid");
        this.sectionname = (String) initdata.get("sectionname");
        this.deptid = (String) initdata.get("deptid");
        this.deptname = (String) initdata.get("deptname");
        this.remark = (String) initdata.get("remark");
        this.adid = (String) initdata.get("adid");
        this.taskname = (String) initdata.get("taskname");
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> execute() throws Exception {
        // 1. 生成和保存三级审批文书
        Apl_admre_audit auditObj = this.businessApproveService.createThreeLevelApprove(super.applyid, this.userid, this.username, Tp_proctype.HEAR_RESTORE);
        Apl_main faMainObj = this.apl_mainService.getById(auditObj.getApplyid());

        // 2. 删除一个子状态
        Apl_admreview admreObj = this.apl_admreviewService.getById(auditObj.getApplyid());
        String sonStatus = this.businessApproveService.delSonStatus(admreObj.getSonstatus(), Tp_proctypeEnum.HEAR_RESTORE.getStatus());

        admreObj.setSonstatus(sonStatus);
        // 更新主表状态为未中止
        admreObj.setIsrest(YesNo.NO);
        this.apl_admreviewService.saveOrUpdate(admreObj);

        // 3. 更新子流程主表状态为未中止
        this.apl_admre_auditDao.updateIsrest(auditObj.getApplyid(), YesNo.NO);

        // 4. 读取流程变量, 更新审理办结时限
        ResponseDTO res = this.iPersonalFeign.getProcessVariable(faMainObj.getInstid());
        if (0 != res.getCode()) {
            throw new RuntimeException(res.getMsg());
        }
        Map<String, Object> variableMap = (Map<String, Object>) res.getData();
        if(!variableMap.containsKey(ProcessVariablesVo.RESTDATE)) {
            throw new RuntimeException("未查询到中止日期");
        }
        String restVar = (String) variableMap.get(ProcessVariablesVo.RESTDATE);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 计算中止日期到现在相差多少天, 给办结时限和期满日期加回对应天数
        Date resdate = sdf.parse(restVar);
        int differNum = (int) ((new Date().getTime() - resdate.getTime()) / (1000*3600*24));

        Apl_limit_hear hearObj = this.apl_limit_hearDao.getById(auditObj.getApplyid());
        Calendar cal = Calendar.getInstance();
        cal.setTime(hearObj.getEnddate());
        cal.add(Calendar.DATE, differNum + 1);

        // 5. 更新审理时限的截止日期
        this.apl_limit_hearDao.updateEnddate(auditObj.getApplyid(), cal.getTime());
        // 6. 更新业务主表期满日期
        this.apl_admreviewService.updateQuedate(faMainObj.getId(), new SimpleDateFormat("yyyy年M月d日").format(cal.getTime()));
        // 6.1 更新业务从表期满日期
        this.apl_admre_auditService.updateQuedate(faMainObj.getId(), new SimpleDateFormat("yyyy年M月d日").format(cal.getTime()));
        // 7. 更新流程办结时限
        this.lLog_limitscatFeign.updateById(faMainObj.getInstid(), faMainObj.getScatid(), sdf.format(cal.getTime()));

        // 刷新文书日期
        this.businessApproveService.processEndedUpdatePreviewDoc(auditObj.getApplyid(), Tp_proctype.HEAR_RESTORE, null);
        // 文书盖章
        this.businessApproveService.templateStamp(auditObj.getApplyid(), Tp_proctype.HEAR_RESTORE);
        return null;
    }

}
